package com.yjs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.bean.Goods;
import com.yjs.bean.History;
import com.yjs.bean.Store;
import com.yjs.mapper.GoodsMapper;
import com.yjs.service.GoodsService;
import com.yjs.service.HistoryService;
import com.yjs.service.StoreService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 货物服务实现层
 *
 * @author yjs
 * @create 2020-09-17-10:07
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    //注入货物mapper
    @Autowired
    GoodsMapper goodsMapper;
    //注入历史service
    @Autowired
    HistoryService historyService;
    //注入仓储service
    @Autowired
    StoreService storeService;

    //查询所有货物
    @Override
    public List<Goods> getAllGoods() {
        List<Goods> allGoods = goodsMapper.getAllGoods();
        if (allGoods.size()>0 && allGoods != null){
            return allGoods;
        }
        return null;
    }
    //新增所有货物并将数据插入到历史记录中
    @Override
    public void addGoods(Goods goods,String username) {
        String gid = UUID.randomUUID().toString();
        goods.setGid(gid);
        goodsMapper.addGoods(goods);
        //插入历史记录
        History history = new History();
        history.setHgsmall(goods.getGsmall());
        history.setHgname(goods.getGname());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datatime = sdf.format(new Date());
        history.setHdatetime(datatime);
        history.setHgtype("新增");
        history.setHgunit(goods.getGsun());
        Goods oneGoods = goodsMapper.getOneGoods(gid);
        history.setHamount(goods.getGsun());
        history.setHsname(oneGoods.getStore().getSname());
        history.setHusername(username);
        historyService.addHistory(history);
    }
    //删除货物并将记录插入到历史记录中
    @Override
    public void delGoods(String gid,String username) {
        //插入历史记录
        Goods oneGoods = goodsMapper.getOneGoods(gid);
        History history = new History();
        history.setHgsmall(oneGoods.getGsmall());
        history.setHgname(oneGoods.getGname());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datatime = sdf.format(new Date());
        history.setHdatetime(datatime);
        history.setHgtype("删除");
        history.setHgunit(oneGoods.getGsun());
        history.setHamount(0);
        history.setHsname(oneGoods.getStore().getSname());
        history.setHusername(username);
        historyService.addHistory(history);
        goodsMapper.delGoods(gid);
    }
    //查询一个货物
    @Override
    public Goods getOneGoods(String gid) {
        Goods oneGoods = goodsMapper.getOneGoods(gid);
        return oneGoods!=null?oneGoods:null;
    }
    //修改货物并将数据插入到历史记录中
    @Override
    public void updGoods(Goods goods,String username) {
        Goods oneGoods = goodsMapper.getOneGoods(goods.getGid());
        Store oneStore = storeService.getOneStore(goods.getStore().getSid());
        Integer oldsun = oneGoods.getGsun();//当前库存数量
        Integer newsun = goods.getGsun();//修改后的库存
        goodsMapper.updGoods(goods);
        //判断是入库还是出库
        if (newsun>oldsun){
            //入库历史记录
            History history = new History();
            history.setHgsmall(goods.getGsmall());
            history.setHgname(goods.getGname());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datatime = sdf.format(new Date());
            history.setHdatetime(datatime);
            history.setHgtype("入库");
            history.setHgunit(newsun-oldsun);
            history.setHamount(newsun);
            history.setHsname(oneStore.getSname());
            history.setHusername(username);
            historyService.addHistory(history);
        }
            //出库历史记录
            History history = new History();
            history.setHgsmall(goods.getGsmall());
            history.setHgname(goods.getGname());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datatime = sdf.format(new Date());
            history.setHdatetime(datatime);
            history.setHgtype("出库");
            history.setHgunit(oldsun-newsun);
            history.setHamount(newsun);
            history.setHsname(oneStore.getSname());
            history.setHusername(username);
            historyService.addHistory(history);
    }
}
